var pages = 1;
$(function() {
	var thisLd = CD.geturl().id;
	categoryOfProduct({
		goodsType: thisLd,
		pages: 1
	}, function(data) {
		console.log(data);
		$('#categotyProduct').html(template('categotyProductTemplate', data));
		upmoreProduct();

		//页面查看详情
		$('.categotyProduct-list-left').on('tap', function() {
			var newId = $(this).parent().attr('data-id');
			console.log(newId);
			window.location.href = "v-shop-product.html?id=" + newId +"&business="+CD.geturl().business;
		});

		//删除分类中的商品
		$('.myCategoryTrash').on('tap',function() {
			var that = $(this);
			var newImgnum = $(this).attr('data-imgnum');
			var newId = $(this).parent().parent().attr('data-id');
			$('.buy-delete').stop().fadeIn();
			$('#buy-delete-close').click(function() {
				$('.buy-delete').stop().fadeOut();
			});
			$('#buy-delete-open').click(function() {
				categoryOfDelete({
					goodsId: newId,
					datailsImg: newImgnum
				}, function() {
					that.parent().parent().stop().fadeOut();
				});
				$('.buy-delete').stop().fadeOut();
			});
		});

		//修改分类中商品
		$('.myCategoryEdit').on('tap', function() {
			var newId = $(this).parent().parent().attr('data-id');
			window.location.href = "v-store-editbox.html?id=" + newId+"&business="+CD.geturl().business;
		});
	});

});

//分类商品的删除
var categoryOfDelete = function(params, callback) {
	$.ajax({
		type: "get",
		async: false,
		cache: false,
		traditional: true,
		url: window.address + "/goods/deleteBayGoodsId",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: "json",
		success: function(data) {
			console.log(data)
			callback && callback(data);
		}
	});
}

//分类中商品遍历
var categoryOfProduct = function(params, callback) {
	$.ajax({
		type: "get",
		async: false,
		cache: false,
		traditional: true,
		url: window.address + "/goods/findBayGoodsType",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: "json",
		success: function(data) {
			callback && callback(data);
		}
	});
}

//上拉加载
var upmoreProduct = function() {
	mui.init({
		pullRefresh: {
			container: "#mui-scroll-wrapper",
			up: {
				callback: function() {
					var that = this;
					pages++;
					console.log(pages, 123)
					categoryOfProduct({
						pages: pages,
						goodsType: CD.geturl().id
					}, function(data) {
						$('#categotyProduct').append(template('categotyProductTemplate', data));

						//页面查看详情
						$('.categotyProduct-list-left').on('tap', function() {
							var newId = $(this).parent().attr('data-id');
							console.log(newId);
							window.location.href = "v-shop-product.html?id=" + newId;
						});

						//删除分类中的商品
						$('.myCategoryTrash').on('tap',function() {
							var that = $(this);
							var newImgnum = $(this).attr('data-imgnum');
							var newId = $(this).parent().parent().attr('data-id');
							$('.buy-delete').stop().fadeIn();
							$('#buy-delete-close').click(function() {
								$('.buy-delete').stop().fadeOut();
							});
							$('#buy-delete-open').click(function() {
								categoryOfDelete({
									goodsId: newId,
									datailsImg: newImgnum
								}, function() {
									that.parent().parent().stop().fadeOut();
								});
								$('.buy-delete').stop().fadeOut();
							});
						});

						//修改分类中商品
						$('.myCategoryEdit').on('tap', function() {
							var newId = $(this).parent().parent().attr('data-id');
							window.location.href = "v-store-editbox.html?id=" + newId;
						});
						if(data.bayGoodsType.length) {
							that.endPullupToRefresh();
						} else {
							that.endPullupToRefresh(true);
						};

					});
				}
			}
		}
	});
}